'use strict';

const Service = require('egg').Service;

class SearchService extends Service {
  async getSearch(keyword, order_name, order, page_num, page_size) {
    const { app } = this;
    const start = (page_num - 1) * page_size;
    const end = Number(page_size)
    keyword = `%${keyword}%`;
    let sql = '';
    if (order_name === "价格") {
      if (order !== "desc") {
        sql = "SELECT * FROM goods WHERE text LIKE  ? ORDER BY price1 LIMIT ?,?"
      } else {
        sql = "SELECT * FROM goods WHERE text LIKE  ? ORDER BY price1 DESC LIMIT ?,?"
      }
    } else {
      if (order !== "desc") {
        sql = "SELECT * FROM goods WHERE text LIKE  ? ORDER BY discount LIMIT ?,?"
      } else {
        sql = "SELECT * FROM goods WHERE text LIKE  ? ORDER BY discount DESC LIMIT ?,?"
      }
    }
    return await app.mysql.query(sql, [keyword, start, end]);
  }
}
   
module.exports = SearchService;
